<template>
	<el-card>
		<el-form ref="form" label-width="80px">

			<el-form-item label="考试名称:">
				<el-select v-model="shijuanId" placeholder="请选择考试科目" @change="change()">
					<el-option v-for="(item,index) in list" :key="item.id" :label="item.name" :value="item.id">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="考试时间:">
				<span>60分钟</span>
			</el-form-item>

			<el-form-item v-if="isKao">
				<el-button v-if="shijuanId" type="primary" @click="ksxm()">进入考试</el-button>
			</el-form-item>
			<div v-if="aa">
			<span >您已考过该试卷，您的考试成绩为：</span><span style="color: red;">{{grade.grade}}</span><span>分，试卷及格线为：</span><span style="color: red;" v-text="grade.shijuan.jige"></span>
				<span>{{msg}}</span>
				</div>
		</el-form>
	</el-card>
</template>
<script>
	export default {
		data() {
			return {
				fullscreenLoading: false,
				shijuanId: '',
				list: [],
				timu: [],
				isKao:false,
				aa:false,
				grade:"",
				msg:'',
			}
		},
		methods: {
			ksxm() {		
				const loading = this.$loading({
					lock: true,
					text: '正在努力加载试卷,即将开始考试！',
					spinner: 'el-icon-loading',
					background: 'rgba(0, 0, 0, 0.7)'
				});
				this.$ajax.post("/Shijuan/chouti", {
					"id": this.shijuanId
				}).then(res => {
					var result = res.data
					console.log(result);
					if (result.success) {
						this.timu = result.data
						window.sessionStorage.setItem("shijuanId", this.shijuanId)
						window.sessionStorage.setItem("timu", JSON.stringify(this.timu))
						this.$router.push('/exam/ksym')
						loading.close()

					}else{
						this.$message.error("试卷格式错误！")
						loading.close()
					}
				})




			},
			change() {
				const loading = this.$loading({
					lock: true,
					text: '正在努力查找试卷，请稍后！',
					spinner: 'el-icon-loading',
					background: 'rgba(0, 0, 0, 0.7)'
				});
				this.$ajax.post("/Grade/find",{"shijuanId":this.shijuanId}).then(res=>{
					console.log(res.data.success);
					if(!res.data.data){
						this.isKao=true
						this.aa=false
						loading.close()
					}else{
						
						this.isKao=false
						this.grade=res.data.data
						this.$nextTick(()=>{
							if(this.grade.grade<this.grade.shijuan.jige){
								this.msg="评判为不合格，请您到手机端进行补考！"
								
							}else{
								this.msg="评判为合格，恭喜您！"
							}
								this.aa=true
							
						})
						loading.close()
					}
				})
			}
		},
		created() {
			window.sessionStorage.removeItem("timu")
			window.sessionStorage.removeItem("shijuanId")
			this.$ajax.post("/Shijuan/list", {}).then(res => {
				this.list = res.data.data
			})
			
		}
	}
</script>
